package com.cloudm.technician.data.db

import com.cloudm.technician.data.model.ImageInfo
import com.cloudm.technician.data.model.User
import io.realm.Realm
import io.realm.RealmResults
import java.util.*

/**
 * 作者: wesley
 * 时间: 2017/11/20
 * 描述: 数据库的接口实现
 */
class AppDbHelper(private val realm: Realm) : DbHelper {

    override fun insertOrUpdateUser(user: User) {
        realm.executeTransaction { realm -> realm.insertOrUpdate(user) }
    }


    override fun queryUserInfoByToken(token: String?): User? {
        return realm.where(User::class.java).equalTo("token", token).findFirst()
    }


    override fun updateUserNickName(user: User?, name: String) {
        realm.executeTransaction { realm ->
            user?.let {
                it.nickname = name
                realm.insertOrUpdate(it)
            }
        }
    }

    override fun updateUserLogo(user: User?, logo: String) {
        realm.executeTransaction { realm ->
            user?.let {
                it.logo = logo
                realm.insertOrUpdate(it)
            }
        }
    }

    override fun insertOrUpdateImageInfo(list: ArrayList<ImageInfo>) {
        realm.executeTransaction { realm ->
            realm.insertOrUpdate(list)
        }
    }


    override fun updateLocalImage(token: Int, tag: Int, deviceId: Int, position: Int, url: String,
                                  flag: Int) {
        val list = queryLocalImage(token, tag, deviceId)
        if (list.size > position) {
            realm.executeTransaction { realm ->
                val info = list[position]
                info.flag = flag
                info.url = url
                realm.insertOrUpdate(info)
            }
        }
    }

    override fun clearLocalImage(token: Int, tag: Int, deviceId: Int) {
        val list = queryAllImageInfoByDeviceId(token, tag, deviceId)
        list.deleteAllFromRealm()
    }

    override fun queryAllImageInfoByDeviceId(token: Int, tag: Int,
                                             deviceId: Int): RealmResults<ImageInfo> {
        return realm.where(ImageInfo::class.java).equalTo("userId", token).equalTo("tag",
                tag).equalTo("deviceId", deviceId).findAll()
    }

    override fun queryLocalImage(token: Int, tag: Int, deviceId: Int): ArrayList<ImageInfo> {
        val result = queryAllImageInfoByDeviceId(token, tag, deviceId)
        val arrayList = arrayListOf<ImageInfo>()
        val list = realm.copyFromRealm(result)
        if (list == null || list.size == 0) {
            val currentTime = System.currentTimeMillis()
            when (tag) {
                0 -> {
                    val image_one = ImageInfo(currentTime + 1, 0, "", "云盒子\n近景照", tag, token,
                            deviceId)
                    arrayList.add(image_one)
                    val image_two = ImageInfo(currentTime + 2, 0, "", "取电线束", tag, token, deviceId)
                    arrayList.add(image_two)
                    val image_three = ImageInfo(currentTime + 3, 0, "", "整车铭牌\n近景照", tag, token,
                            deviceId)
                    arrayList.add(image_three)
                    val image_four = ImageInfo(currentTime + 4, 0, "", "车牌号照片", tag, token,
                            deviceId)
                    arrayList.add(image_four)
                    val image_five = ImageInfo(currentTime + 5, 0, "", "设备全身照", tag, token,
                            deviceId)
                    arrayList.add(image_five)
                    val image_six = ImageInfo(currentTime + 6, 0, "", "发动机照片\n近景照", tag, token,
                            deviceId)
                    arrayList.add(image_six)
                }

                1 -> {
                    val image_one = ImageInfo(currentTime + 7, 0, "", "液位节点\n近景照", tag, token,
                            deviceId)
                    arrayList.add(image_one)
                    val image_two = ImageInfo(currentTime + 8, 0, "", "液位线束", tag, token, deviceId)
                    arrayList.add(image_two)
                }
            }
            insertOrUpdateImageInfo(arrayList)
        } else {
            (0 until list.size).mapTo(arrayList) { list[it] }
        }
        return arrayList
    }

    override fun deleteAllData() {
        realm.executeTransaction { realm ->
            realm.deleteAll()
        }
    }
}